<?php
namespace app\home\controller;

use app\BaseController;
use think\db\Where;
use think\facade\Db;
use think\facade\Session;
use think\facade\View;

class Index extends BaseController
{
    //登录信息
    public function userinformation(){
        $user=[
            'img'=>Session::get('archimg'),
            'name'=>Session::get('archname'),
            'jian'=>Session::get('arcbrief'),
            'id'=>Session::get('archid')
        ];
        return $user;
    }
    //登陆状态验证
    public function login(){
        if(!Session::has('archives')){
            header('Location:http://tptest.com/home/login');
        }
    }
    //网站首页
    public function index(){
        $this->login();
            $info = input('select');
            $userid=Session::get('archid');
            //置顶查询
            $sel = Db::name('article')
                ->alias('a')
                ->join(['cate' => 'c'], 'a.cateid=c.id')
                ->join(['collection'=>'c2'],"a.id=c2.article_id and c2.archives_id='$userid'",'left')
                ->join(['archives'=>'a2'],'c2.archives_id=a2.u_id','left')
                ->field('a.id,a.title,a.thumb,c.catename,a.description,a.author,date(a.pubdate) as pubdate,clicks,favs,c2.article_id as artid')
                ->where('istop', 1)
                ->where('a.isdeleteed',1)
                ->order('clicks','desc')
                ->select();

            if($info==''){
                $sels = Db::name('article')
                    ->alias('a')
                    ->join(['cate' => 'c'], 'a.cateid=c.id')
                    ->join(['collection'=>'c2'],"a.id=c2.article_id and c2.archives_id='$userid'",'left')
                    ->join(['archives'=>'a2'],"c2.archives_id=a2.u_id",'left')
                    ->field('a.id,a.title,a.thumb,c.catename,a.description,a.author,date(a.pubdate) as pubdate,clicks,favs,c2.article_id as artid')
                    ->where('istop',0)
                    ->limit(0,8)
                    ->where('a.isdeleteed',1)
                    ->order('pubdate', 'desc')
                    ->order('clicks','desc')
                    ->select();
            }else{
                $sels = Db::name('article')
                    ->alias('a')
                    ->join(['cate' => 'c'], 'a.cateid=c.id')
                    ->join(['collection'=>'c2'],"a.id=c2.article_id and c2.archives_id='$userid'",'left')
                    ->join(['archives'=>'a2'],"c2.archives_id=a2.u_id",'left')
                    ->field('a.id,a.title,a.thumb,c.catename,a.description,a.author,date(a.pubdate) as pubdate,clicks,favs,c2.article_id as artid')
                    ->whereLike('title',"%$info%")
                    ->limit(0,8)
                    ->where('a.isdeleteed',1)
                    ->order('pubdate', 'desc')
                    ->order('clicks','desc')
                    ->select();
            }
            //点击量排行
            $clph=Db::name('article')->field('title')->order('clicks','desc')->limit(10)->select();
            View::assign('clph',$clph);
            //最近心情
            $mood=Db::name('mood')->field('mood,date(pubdate)')->select();
            View::assign('mood',$mood);
            //用户信息
            $user=$this->userinformation();
            View::assign('user',$user);

            View::assign('info', $info);
            View::assign('sel', $sel);
            View::assign('sels', $sels);
            return View::fetch();
    }
    //下拉数据
    public function xiala(){
        $info = input('select');
        $userid=Session::get('archid');
        $date=Db::name('article')
            ->alias('a')
            ->join(['cate' => 'c'], 'a.cateid=c.id')
            ->join(['collection'=>'c2'],"a.id=c2.article_id and c2.archives_id='$userid'",'left')
            ->join(['archives'=>'a2'],"c2.archives_id=a2.u_id",'left')
            ->field('a.id,a.title,a.thumb,c.catename,a.description,a.author,date(a.pubdate) as pubdate,clicks,favs,c2.article_id as artid')
            ->whereLike('title',"%$info%")
            ->where('istop',0)
            ->limit(input('limit'),5)
            ->where('a.isdeleteed',1)
            ->order('pubdate', 'desc')
            ->order('clicks','desc')
            ->select()
            ->toArray();
        return json($date);
    }
    //博客下拉数据
    public function xiala1(){
        $info = input('select');
        $userid=Session::get('archid');
        $date=Db::name('article')
            ->alias('a')
            ->join(['cate' => 'c'], 'a.cateid=c.id')
            ->join(['collection'=>'c2'],"a.id=c2.article_id and c2.archives_id='$userid'",'left')
            ->join(['archives'=>'a2'],"c2.archives_id=a2.u_id",'left')
            ->field('a.id,a.title,a.thumb,c.catename,a.description,a.author,date(a.pubdate) as pubdate,clicks,favs,c2.article_id as artid')
            ->whereLike('title',"%$info%")
            ->limit(input('limit'),5)
            ->where('a.isdeleteed',1)
            ->order('pubdate', 'desc')
            ->order('clicks','desc')
            ->select()
            ->toArray();
        return json($date);
    }
    //随言碎语
    public function sysy(){
        $this->login();
        //用户信息
        $user=$this->userinformation();
        View::assign('user',$user);
        //随言碎语查询
        $sysy=Db::name('mood')->field('mood,date(pubdate) as date,zt')->select();
        View::assign('sysy',$sysy);
        return View::fetch();
    }
    //博客日记
    public function bkrj(){
        $this->login();
        $userid=Session::get('archid');
        $info=input('select');
        $date=Db::name('article')
            ->alias('a')
            ->join(['cate' => 'c'], 'a.cateid=c.id')
            ->join(['collection'=>'c2'],"a.id=c2.article_id and c2.archives_id='$userid'",'left')
            ->join(['archives'=>'a2'],"c2.archives_id=a2.u_id",'left')
            ->field('a.id,a.title,a.thumb,c.catename,a.description,a.author,date(a.pubdate) as pubdate,clicks,favs,c2.article_id as artid')
            ->whereLike('title',"%$info%")
            ->limit(0,11)
            ->where('a.isdeleteed',1)
            ->order('pubdate', 'desc')
            ->order('clicks','desc')
            ->select();
        View::assign('date',$date);
        //标签云
        $labyun=Db::name('label')->select();
        View::assign('labyun',$labyun);
        //站长推荐
        $tuij=Db::name('article')->field('id,thumb,title')->limit(3)->order('clicks','desc')->select();
        View::assign('tuij',$tuij);
        //用户信息
        $user=$this->userinformation();
        View::assign('sel',$info);
        View::assign('user',$user);
        return View::fetch();
    }
    //我的相册
    public function wdxc(){
        $this->login();
        //用户信息
        $user=$this->userinformation();
        View::assign('user',$user);
        //相册查询
        $album=Db::name('album')->select();
        View::assign('album',$album);
        return View::fetch();
    }
    public function xiangce(){
        $this->login();
        //用户信息
        $user=$this->userinformation();
        View::assign('user',$user);
        //图片显示
        $aid=input('pid');
        $photos=Db::name('al_photo')->field('src')->where('album_id',$aid)->select();
        View::assign('photos',$photos);
        return View::fetch();
    }
    //时间轴
    public function sjz(){
        $this->login();
        //查询
        $dia=Db::name('diary')->field('date(pubdate) as date,title')->limit(10)->order('pubdate','desc')->select();
        View::assign('dia',$dia);
        //用户信息
        $user=$this->userinformation();
        View::assign('user',$user);
        return View::fetch();
    }
    //关于博主
    public function gybz(){
        $this->login();
        //用户信息
        $user=$this->userinformation();
        View::assign('user',$user);
        return View::fetch();
    }
    //留言
    public function ly(){
        $this->login();
        //用户信息
        $user=$this->userinformation();
        View::assign('user',$user);
        //留言查询
        $liuyan=Db::name('liuyan')
            ->alias('l')
            ->join(['archives'=>'a'],'l.arch_id=a.u_id')
            ->field('l.title,l.pubdate,a.u_name,a.u_portrait as src')
            ->order('pubdate','desc');
        $date=$liuyan->paginate(3);
        $page=$date->render();
        View::assign('date',$date);
        View::assign('page',$page);
        return View::fetch();
    }
    //留言添加
    public function lyadd(){
        $data=[
            'title'=>input('title'),
            'arch_id'=>input('id')
        ];
        $date=Db::name('liuyan')->insert($data);
        if($date){
            return json(['status'=>'1','msg'=>'回复成功']);
        }else{
            return json(['status'=>'2','msg'=>'回复失败']);
        }
    }
    //文章详情页
    public function info(){
        //用户信息
        $user=$this->userinformation();
        View::assign('user',$user);
//站长推荐
        $tuij=Db::name('article')->field('id,thumb,title')->where('recommend',1)->limit(3)->order('clicks','desc')->select();
        View::assign('tuij',$tuij);
        //文章查询
        $userid=Session::get('archid');
        $titleid=input('titleid');
        $date=Db::name('article')
            ->alias('a')
            ->join(['cate'=>'c'],'a.cateid=c.id')
            ->join(['collection'=>'c2'],"a.id=c2.article_id and c2.archives_id='$userid'",'left')
            ->join(['archives'=>'a2'],'c2.archives_id=a2.u_id','left')
            ->field('a.id,a.title,a.thumb,c.catename,a.description,a.author,date(a.pubdate) as pubdate,content,clicks,favs,keywords,c2.article_id as artid')
            ->where('a.id',$titleid)
            ->find();
            View::assign('date',$date);
        //标签查询
        $lab=explode(',',$date['keywords']);
        $labinfo=[];
        foreach ($lab as $key =>$val){
            $label=Db::name('label')->field('l_name')->where('l_id',$val)->find();
            $labinfo[$key]=$label['l_name'];
        }
        View::assign('label',$labinfo);
        //相关文章查询
        $keywords=$lab;
        foreach ($keywords as &$val){
            $val='%'.$val.'%';
        }
        $relevant=Db::name('article')->field('title,id')->whereLike('keywords',$keywords,'or')->limit(4)->select();
        View::assign('relevant',$relevant);
        //点击量增加
        $data=[
            'id'=>$titleid,
            'clicks'=>$date['clicks']+1
        ];
        $click=Db::name('article')->update($data);
        //标签云
        $labyun=Db::name('label')->select();
        View::assign('labyun',$labyun);

        //上一页下一页查询
        $previouspage=Db::name('article')
            ->field('title,id')
            ->where('isdeleteed',1)
            ->where('id','<',$titleid)
            ->order('id','desc')
            ->find();
            View::assign('prepage',$previouspage);
        $nextpage=Db::name('article')
            ->field('title,id')
            ->where('isdeleteed',1)
            ->where('id','>',$titleid)
            ->order('id','asc')
            ->find();
            View::assign('nextpage',$nextpage);
        return View::fetch();
    }
    public function do_like(){
        $favs=Db::name('article')->field('favs')->where('id',input('id'))->find();
        $fav=$favs['favs'];
        if (input('shu')==0){
            $shu=$fav-1;
            $zhuangtai=1;
            $del=Db::name('collection')->where('article_id',input('id'))->where('archives_id',Session::get('archid'))->delete();
        }else{
            $zhuangtai=0;
            $shu=$fav+1;
                $zu=[
                    'article_id'=>input('id'),
                    'archives_id'=>Session::get('archid')
                ];
                $addcoll=Db::name('collection')->insert($zu);
        }
        $date=[
            'id'=>input('id'),
            'favs'=>$shu
        ];
        $update=Db::name('article')->update($date);
        return json(['zt'=>$zhuangtai,'val'=>$favs['favs']]);
    }
}